package com.scaffold.chapter.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import wangjg.commons.page.Page;

import com.scaffold.chapter.dao.ChapterDAO;
import com.scaffold.chapter.entity.Chapter;

@Service
@Transactional
public class ChapterService {

	@Autowired
	private ChapterDAO chapterDAO;
	
	public Chapter getChapter(long id ) {
		return this.chapterDAO.findById(id);
	}

	public void save(Chapter chapter){
		Date now = new Date();
		if(chapter.getId()==null){
			chapter.setCreated(now);
			chapter.setModified(now);
			this.chapterDAO.save(chapter);
		}else{
			chapter.setModified(now);
			this.chapterDAO.update(chapter);
		}
	}

	public void update(Chapter chapter){
		Date now = new Date();
		chapter.setModified(now);
		this.chapterDAO.update(chapter);
	}

	public void delete(Chapter chapter){
		this.chapterDAO.delete(chapter);
	}
	
	public List<Chapter> pageQuery(Page page, Long topicId, Long catalogId, String keyword){
		return this.chapterDAO.pageQuery(page, topicId, catalogId, keyword);
	}

}
